import {take,call,put,select} from 'redux-saga/effects'
import {get, post, put_http} from '../axios'
import {actionsTypes as IndexActionTypes} from '../reducers'
import {actionsTypes as AssiginActionTypes} from '../reducers/assign'
import history from '../history';

export function* setSelectedFlow () {
    while (true){
        let req = yield take(AssiginActionTypes.SET_SELECTED);
        let res = req.data
        yield put({type: AssiginActionTypes.RESPONSE_SELECTED, data: res})
    }
}

function* assign(project_id, data) {
    yield put({type: IndexActionTypes.FETCH_START});
    try {
        return yield call(post, `mem/project-publish-to-execute/${project_id}`, data);
    } catch (err) {
        yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: '网络请求错误', msgType: 'fail'});
    } finally {
        yield put({type: IndexActionTypes.FETCH_END})
    }
}

export function* assignFlow () {
    while (true) {
        let req = yield take(AssiginActionTypes.ASSIGN);
        let project_id = req.project_id,
            data = req.data
        
        let res = yield call(assign, project_id, data);
        if (res && res.success) {
            yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: '分配成功', msgType: 'success'});
            yield call( ()=>setTimeout( function () {
                history.replace(`/project-detail/introduction/publish?type=publish&id=${peoject_id}&project_id=${project_id}`)
            }, 1000))
        } else {
            yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: res.msg, msgType: 'fail'});
        }
    }
}